package com.johndriver.selenium.run;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class SeleniumRun {
    public static WebDriver driver(){
        System.setProperty("webdriver.chrome.driver", "src\\main\\resources\\static\\chromedriver.exe");
        return new ChromeDriver();
    }

    final static WebDriver driver = driver();

    public static void main(String[] args) throws InterruptedException {
        chrome();
    }

    private static void chrome() throws InterruptedException {
        driver.manage().window().maximize();
        driver.get("https://www.ianzhang.cn/bing/");
        driver.switchTo().alert().accept();
        Thread.sleep(5000);
        driver.switchTo().frame("bing");
        driver.findElement(By.id("sb_form_q")).sendKeys("Bing" + Keys.ENTER);
        Thread.sleep(5000);
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
        try {
            WebElement element = driver.findElement(By.xpath("//*[@id=\"b_results\"]/li[12]/nav/ul/li[2]/a"));
            element.click();
        } catch (Exception e) {
            WebElement element = driver.findElement(By.xpath("//*[@id=\"b_results\"]/li[13]/nav/ul/li[2]/a"));
            element.click();
        }
        Thread.sleep(5000);

        summary();

        driver.findElement(By.className("b_searchbox")).clear();
        driver.findElement(By.className("b_searchbox")).sendKeys("SC", Keys.ENTER);

        summary();
    }

    private static void summary() {
        Map<String, String> mapText = new HashMap<>();
        for (int i = 1; i < 11; i++) {
            String title, linkText;
            if (i == 6) {
                title = driver.findElement(By.xpath("//*[@id=\"b_results\"]/li[" + i + "]/div[1]/div/div[2]/h2/a")).getText();
                linkText = driver.findElement(By.xpath("//*[@id=\"b_results\"]/li[" + i + "]/div[1]/div/div[3]/cite")).getText();
            } else {
                title = driver.findElement(By.xpath("//*[@id=\"b_results\"]/li[" + i + "]/div[1]/h2/a")).getText();
                linkText = driver.findElement(By.xpath("//*[@id=\"b_results\"]/li[" + i + "]/div[2]/div/cite")).getText();
            }
            mapText.put(title, linkText);
        }
        Set<Map.Entry<String, String>> entries = mapText.entrySet();
        List<String> domain = new ArrayList<>();
        for (Map.Entry<String, String> entire : entries) {
            System.out.println(entire.getKey() + "--->" + entire.getValue());
            Pattern p = Pattern.compile("(?i)^(http|https)?://(?:\\w+\\.)*?(\\w*\\.(?:com\\.cn|cn|com|net))[\\\\\\/]*", Pattern.CASE_INSENSITIVE);
            Matcher matcher = p.matcher(entire.getValue());
            while (matcher.find()) {
                domain.add(matcher.group(2));
            }
        }
        System.out.println(domain);
        Map<String, Integer> map = new HashMap<>();
        for (String str : domain) {
            int i = 1;
            if (map.get(str) != null) {
                i = map.get(str) + 1;
            }
            map.put(str, i);
        }
        Set<Map.Entry<String, Integer>> url = map.entrySet();
        for (Map.Entry<String, Integer> entire : url) {
            System.out.println(entire.getKey() + "--->" + entire.getValue());
        }
    }
}
